﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace AMS.DataAccess
{
    /// <summary>
    /// 一个 DataTable 与业务对象互相转换的辅助。
    /// 业务对象的要操作的属性必需要有 DBAttribute 子类的标注.
    /// 其实现与 <see cref="BusinessObjectMapping"/> 等价
    /// </summary>
    public static class BOConverter
    {
        private static BOMapper mapper = new BOMapper();

        /// <summary>
        /// 将DataTable转换成与之对应的业务对象列表。
        /// </summary>
        /// <typeparam name="T">业务实体，指有DBTableAttribute修饰的实体。</typeparam>
        /// <param name="table">数据库表</param>
        /// <returns>业务对象列表</returns>
        public static List<T> DataTableToBOList<T>(DataTable table)
        {
            return mapper.DataTableToBOList<T>(table);
        }

        /// <summary>
        /// 将一个DataRow转换成一个业务实体对象。
        /// </summary>
        /// <typeparam name="T">业务实体，指有DBTableAttribute修饰的实体。</typeparam>
        /// <param name="row">要转换的数据列</param>
        /// <returns>业务实体对象</returns>
        public static T DataRowToBO<T>(DataRow row)
        {
            return mapper.DataRowToBO<T>(row);
        }

        /// <summary>
        /// 将一个DataTable转换成一个对象的 List
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static List<int> DataTableToIntList(DataTable table)
        {
            return mapper.DataTableToIntList(table);
        }

        /// <summary>
        /// 将一个DataTable的某个列的所有内容转换成一个int数组
        /// </summary>
        /// <param name="table"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        public static List<int> DataTableToIntList(DataTable table, string column)
        {
            return mapper.DataTableToIntList(table, column);
        }

        /// <summary>
        /// 将一个DataTable的第 0 列的所有内容转换成一个int数组
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static List<string> DataTableToStringList(DataTable table)
        {
            return mapper.DataTableToStringList(table);
        }

        /// <summary>
        /// 将一个DataTable的某个列的所有内容转换成一个int数组
        /// </summary>
        /// <param name="table"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        public static List<string> DataTableToStringList(DataTable table, string column)
        {
            return mapper.DataTableToStringList(table, column);
        }

        /// <summary>
        /// 将对象的值转到DataRow对应的值中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bo"></param>
        /// <param name="row"></param>
        public static void BOToDataRow<T>(T bo, DataRow row)
        {
            mapper.BOToDataRow<T>(bo, row);
        }

        /// <summary>
        /// 业务对象列表映射到DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="boList"></param>
        /// <param name="table"></param>
        public static void BOListToRows<T>(List<T> boList, DataTable table)
        {
            mapper.BOListToRows<T>(boList, table);
        }
    }
}
